import logging
import time

from bson.objectid import ObjectId

from tornado.web import RequestHandler
from tornado.web import authenticated

from tornado.gen import coroutine
from tornado.web import asynchronous

from handlers import BaseHandler, UserBaseHandler
from handlers.admin import AdminBaseHandler
'''
apply status
0 初始化
10 审核中
20 审核完成
－10 放弃
－20 拒绝
'''


class MyHandler(UserBaseHandler):

    @coroutine
    def get(self):
        username = self.current_user.get('username')

        cur_apply_ing = self.db.apply.find({"user": username, "status": 0})
        apply_ing = yield cur_apply_ing.to_list(12)

        cur_apply_drop = self.db.apply.find({"user": username, "status": -10})
        apply_drop = yield cur_apply_drop.to_list(12)

        cur_apply_ed = self.db.apply.find({"user": username, "status": 10})
        apply_ed = yield cur_apply_ed.to_list(12)

        self.render('apply/my.html', apply_ed=apply_ed,apply_drop=apply_drop, apply_ing=apply_ing)



class DetailHandler(UserBaseHandler):

    @coroutine
    def get(self, id):
        detail = yield self.db.apply.find_one({"_id": ObjectId(id)})

        self.render('apply/detail.html', detail=detail)


class FormHandler(UserBaseHandler):

    @coroutine
    def get(self, dataset_id):

        detail = yield self.db.dataset.find_one({"_id": ObjectId(dataset_id)})

        if detail:
            self.render('apply/form.html', detail=detail)

    @coroutine
    def post(self, dataset_id):
        title = self.get_body_argument("title", default=None)
        background = self.get_body_argument("background", default=None)
        target = self.get_body_argument("target", default=None)
        content = self.get_body_argument("content", default=None)
        expectation = self.get_body_argument("expectation", default=None)
        expectedsoft = self.get_body_argument("expectedsoft", default=None)
        note = self.get_body_argument("note", default=None)

        dataset = yield self.db.dataset.find_one({"_id": ObjectId(dataset_id)})

        apply_item = {
            "user": self.current_user["username"],
            "reseach": {
                "title": title,
                "initiator":self.current_user["username"],
                "background": background,
                "target": target,
                "content": content,
                "expectation": expectation,
                "note": note,
                "expectedsoft": expectedsoft
            },
            "dataset": dataset,
            "commenttime": time.time(),
            "updatetime": time.time(),
            "status": 0
        }

        id = yield self.db.apply.insert(apply_item)
        self.redirect('/apply/my')


class DropHandler(UserBaseHandler):

    @coroutine
    def get(self, id):
        detail = yield self.db.apply.find_and_modify({"_id": ObjectId(id)}, {
            "$set": {
                "status": -10
            }
        })

        self.redirect('/apply/my')

#######
#AdminHandler
#######

class ListHandler(AdminBaseHandler):

    @coroutine
    def get(self):

        username = self.current_user.get('username')

        cur_apply_ing = self.db.apply.find({"status": 0})
        apply_ing = yield cur_apply_ing.to_list(12)

        cur_apply_ed = self.db.apply.find({"status": 20})
        apply_ed = yield cur_apply_ed.to_list(12)

        cur_apply_refused = self.db.apply.find({"status": -10})
        apply_refused = yield cur_apply_refused.to_list(5)

        self.render('admin/apply/index.html',
                    apply_ed=apply_ed, apply_ing=apply_ing, apply_refused=apply_refused)


class DetailAdminHandler(AdminBaseHandler):

    @coroutine
    def get(self, id):
        detail = yield self.db.apply.find_one({"_id": ObjectId(id)})
        self.render('admin/apply/detail.html', detail=detail)


class RefuseAdminHandler(AdminBaseHandler):

    @coroutine
    def get(self, id):
        detail = yield self.db.apply.find_and_modify({"_id": ObjectId(id)}, {
            "$set": {
                "status": -10
            }
        })
        self.redirect('/admin/apply')

class ResetAdminHandler(AdminBaseHandler):

    @coroutine
    def get(self, id):
        detail = yield self.db.apply.find_and_modify({"_id": ObjectId(id)}, {
            "$set": {
                "status": 0
            }
        })
        self.redirect('/admin/apply')


class AcceptAdminHandler(AdminBaseHandler):

    @coroutine
    def get(self, id):
        detail = yield self.db.apply.find_and_modify({"_id": ObjectId(id)}, {
            "$set": {
                "status": 20
            }
        })

        app_item = {
            "user": self.current_user["username"],
            "dataset": detail.get('dataset'),
            "reseach": detail.get('reseach'),
            "commenttime": time.time(),
            "updatetime": time.time(),
            "status": 0
        }

        yield self.db.apps.insert(app_item)

        self.redirect('/admin/apply')
